import Vue from "vue";
import VueRouter from "vue-router";

Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    component: () => import("@/views/home.vue"),
  },
  {
    path: "/release",
    component: () => import("@/views/release.vue"),
  },
  {
    path: "/user",
    component: () => import("@/views/user.vue"),
  },
  {
    path: "/login",
    component: () => import("@/views/Login.vue"),
  },
  {
    path: "/mySelf",
    component: () => import("@/views/mySelf.vue"),
  },
  {
    path: "/changePassword",
    component: () => import("@/views/changePassword.vue"),
  },
  {
    path: "/coll",
    component: () => import("@/views/Collection.vue"),
  },
];

const router = new VueRouter({
  // mode: "history",
  routes,
});

router.beforeEach((to, from, next) => {
  if (
    to.path == "/user" ||
    to.path == "/changePassword" ||
    to.path == "/mySelf"
  ) {
    const token = localStorage.getItem("token");
    if (token) {
      next();
    } else {
      next("/login");
    }
  } else {
    next();
  }
});

export default router;
